package com.my.mymafengwo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.smallstrong.mafengwopro.R;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.my.mymafengwo.adapter.BaseAdapterHelper;
import com.my.mymafengwo.adapter.ContinentsAdapter;
import com.my.mymafengwo.adapter.QuickAdapter;
import com.my.mymafengwo.base.BaseActivity;
import com.my.mymafengwo.bean.SearchStrategy;
import com.my.mymafengwo.utils.UrlUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * ***********************************************
 * author:ljz
 * time:2015/12/6 19:21
 * name:找攻略页面
 * step:
 * ***********************************************
 */
public class SearchStrategyActivity extends BaseActivity implements View.OnClickListener {
    private GridView gvContinentContent, gvHot;
    private ListView lvContinentName;
    private Button btnBack;
    private RelativeLayout relativeLayout;

    private int index=0;//listView点击的下标
    private List<TextView> TextViewList = new ArrayList<>();
    private List<ImageView> ImageViewList = new ArrayList<>();

    private List<SearchStrategy.DataEntity.ContinentsEntity> listContinentsEntity;//Continents名称集合
    private SearchStrategy.DataEntity.TopEntity top;//热门搜索的实体类对象
    private ContinentsAdapter continentsAdapter;//Continents的适配器

    private List<SearchStrategy.DataEntity.TopEntity.GroupsEntity> listGroupsEntity;//热门搜索的集合
    private QuickAdapter<SearchStrategy.DataEntity.TopEntity.GroupsEntity> GroupsAdapter;//热门搜索的适配器

    private List<SearchStrategy.DataEntity.TopEntity.GroupsEntity.ChildMddsEntity> listChildMddsCity;//热门搜索的城市的集合
    private QuickAdapter<SearchStrategy.DataEntity.TopEntity.GroupsEntity.ChildMddsEntity> ChildMddsCityAdapter;//热门搜索的城市适配器

    private List<SearchStrategy.DataEntity.ContinentsEntity.ChildMddsEntity> listChildMdds;//七大洲搜索的集合
    private QuickAdapter<SearchStrategy.DataEntity.ContinentsEntity.ChildMddsEntity> childMddsAdapter;//七大洲搜索所用的适配器

    @Override
    protected void loadXml() {
        setContentView(R.layout.activity_search_strategy);
        continentsAdapter = new ContinentsAdapter(this);
        TextViewList = continentsAdapter.getTextView();
        ImageViewList = continentsAdapter.getImageView();
    }

    @Override
    protected void getIntentData(Bundle savedInstanceState) {

    }

    @Override
    protected void initViews() {
        btnBack = (Button) findViewById(R.id.btn_back);
        lvContinentName = (ListView) findViewById(R.id.lv_continent_name);
        gvContinentContent = (GridView) findViewById(R.id.gv_continent_content);
        relativeLayout = (RelativeLayout) findViewById(R.id.relativelayout);
    }

    @Override
    protected void registerListener() {
        btnBack.setOnClickListener(this);
        relativeLayout.setOnClickListener(this);
        lvContinentName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {
                    loadHotSearchData();
                } else {
                    loadAsiaData(position);
                }
                    TextViewList.get(index).setTextColor(getResources().getColor(R.color.black));
                    ImageViewList.get(index).setVisibility(View.INVISIBLE);
                    TextViewList.get(position).setTextColor(getResources().getColor(R.color.orange));
                    ImageViewList.get(position).setVisibility(View.VISIBLE);
                    index = position;
            }
        });
    }

    /**
     * 加载热门搜索的数据
     */
    private void loadHotSearchData() {
        gvContinentContent.setNumColumns(1);
        listGroupsEntity = top.getGroups();
        GroupsAdapter = new QuickAdapter<SearchStrategy.DataEntity.TopEntity.GroupsEntity>(this, R.layout.item_hot_search_grid, listGroupsEntity) {
            @Override
            protected void convert(BaseAdapterHelper helper, SearchStrategy.DataEntity.TopEntity.GroupsEntity item) {
                helper.setText(R.id.tv_hot_search, item.getName());
                gvHot = helper.getView(R.id.gv_hot);
                listChildMddsCity = item.getChild_mdds();
                ChildMddsCityAdapter = new QuickAdapter<SearchStrategy.DataEntity.TopEntity.GroupsEntity.ChildMddsEntity>(SearchStrategyActivity.this, R.layout.item_hot_search_city_grid, listChildMddsCity) {
                    @Override
                    protected void convert(BaseAdapterHelper helper, SearchStrategy.DataEntity.TopEntity.GroupsEntity.ChildMddsEntity item) {
                        helper.setText(R.id.tv_hot_city, item.getName());
                    }
                };
                gvHot.setAdapter(ChildMddsCityAdapter);
                gvHot.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        String url = UrlUtils.WEB_CITY_URL + listChildMddsCity.get(position).getId();
                        jumpWebActivity(url);
                    }
                });
            }
        };
        gvContinentContent.setAdapter(GroupsAdapter);

    }

    /**
     * 加载七大洲和中国搜索的数据
     *
     * @param position
     */
    private void loadAsiaData(int position) {
        gvContinentContent.setNumColumns(3);
        listChildMdds = listContinentsEntity.get(position-1).getChild_mdds();
        childMddsAdapter = new QuickAdapter<SearchStrategy.DataEntity.ContinentsEntity.ChildMddsEntity>(this, R.layout.item_strategy_grid, listChildMdds) {
            @Override
            protected void convert(BaseAdapterHelper helper, SearchStrategy.DataEntity.ContinentsEntity.ChildMddsEntity item) {
                helper.setImageUrl(R.id.iv_strategy, item.getThumbnail());
                helper.setText(R.id.tv_strategy, item.getName());
            }
        };
        gvContinentContent.setAdapter(childMddsAdapter);
        gvContinentContent.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String url = UrlUtils.WEB_CITY_URL + listChildMdds.get(position).getId();
                jumpWebActivity(url);
            }
        });
    }

    /**
     * 加载数据
     */
    @Override
    protected void initData() {
        HttpUtils get = new HttpUtils();
        get.send(HttpRequest.HttpMethod.GET, UrlUtils.SEARCH_STRATEGY_URL, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String result = responseInfo.result;
                Gson gson = new Gson();
                SearchStrategy searchStrategy = gson.fromJson(result, SearchStrategy.class);
                listContinentsEntity = searchStrategy.getData().getContinents();
                top=searchStrategy.getData().getTop();
                List<String> list = new ArrayList<String>();
                list.clear();
                for (int i = 0; i < listContinentsEntity.size();i++) {
                    list.add(listContinentsEntity.get(i).getName());
                }
                list.add(0,top.getName());
                continentsAdapter.setData(list);
                continentsAdapter.notifyDataSetChanged();
                lvContinentName.setAdapter(continentsAdapter);
                //先加载热门数据
                loadHotSearchData();
            }

            @Override
            public void onFailure(HttpException e, String s) {
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.relativelayout:
                Intent intent = new Intent(SearchStrategyActivity.this, StartSearchActivity.class);
                intent.putExtra("hint", "搜索当地吃喝玩乐");
                startActivity(intent);
                break;
        }
    }

    /**
     * 跳转到TravelNotesActivity
     */
    public void jumpWebActivity(String url) {
        Intent intent = new Intent(SearchStrategyActivity.this, TravelNotesActivity.class);
        intent.putExtra("url", url);
        startActivity(intent);
    }
}
